﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MainClient
{
    public partial class frmUpdPwd : Form
    {
        private string editUserName;

        public frmUpdPwd(string editUserName)
        {
            InitializeComponent();
            this.editUserName = editUserName.Trim();
        }

        private void frmUpdPwd_Load(object sender, EventArgs e)
        {
            this.gb_loginuser.Text = "请再输入用户:"+LoginUser.getCurrOper()+" 的密码";
            this.gb_edituser.Text = "修改用户: "+this.editUserName+" 的密码";
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            DataAccess.UserDAO userDAO = new DataAccess.UserDAO();
            if (userDAO.isUserExist(LoginUser.getCurrOper(), this.txt_loginPass.Text.Trim()))
            {
                if (this.txt_newPwd.Text.Trim() == this.txt_newPwd2.Text.Trim())
                {
                    userDAO.updatePwd(this.editUserName, this.txt_newPwd.Text.Trim());
                    MessageBox.Show("密码修改成功！", "信息",
                                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("两次输入的密码不一致，请重新输入！", "错误",
                                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.txt_newPwd2.Text = "";
                    this.txt_newPwd.Text = "";                    
                }
            } else
            {
                MessageBox.Show("登录用户密码有误，请重新输入!", "错误",
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                clearAllInput();
                return;
            }

        } // end of function

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void clearAllInput()
        {
            this.txt_loginPass.Text = "";
            this.txt_newPwd.Text = "";
            this.txt_newPwd2.Text = "";
            this.txt_loginPass.Select();
        }
    }
}
